<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>{title}</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        #jump-input {
            background: rgba(255, 255, 255, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            padding: 2px 5px;
            border-radius: 4px;
            width: 60px;
            font-family: Arial, sans-serif;
        }

        #jump-input:focus {
            outline: none;
            box-shadow: 0 0 3px rgba(255, 255, 255, 0.5);
        }

        body {
            height: 100vh;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            overflow: hidden;
        }

        .back-btn {
            position: fixed;
            top: 10px;
            left: 10px;
            color: white;
            text-decoration: none;
            background-color: rgba(0, 0, 0, 0.5);
            padding: 5px 10px;
            border-radius: 4px;
            font-family: Arial, sans-serif;
            font-size: 14px;
            z-index: 1200;
        }

        .info-box {
            position: fixed;
            top: 20px;
            left: 20px;
            background: rgba(0, 0, 0, 0.3);
            color: white;
            padding: 10px 20px;
            border-radius: 8px;
            font-family: Arial, sans-serif;
            backdrop-filter: blur(5px);
            z-index: 1000;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
        }

        @media (max-width: 768px) {
            .info-box {
                top: auto;
                bottom: 20px;
                left: 50%;
                transform: translateX(-50%);
                width: 80%;
                text-align: center;
            }

            .back-btn {
                top: auto;
                bottom: 80px;
                left: 50%;
                transform: translateX(-50%);
            }
        }

        .container {
            position: relative;
            height: 100vh;
            width: 100vw;
        }

        .slide {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            opacity: 0;
            z-index: 0;
        }

        .slide.active {
            opacity: 1;
            z-index: 1;
        }

        .slide img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            user-select: none;
            -webkit-user-select: none;
            -webkit-touch-callout: none;
        }

        .swipe-indicator {
            position: fixed;
            height: 100%;
            width: 50px;
            top: 0;
            background-color: rgba(255, 255, 255, 0.1);
            opacity: 0;
            transition: opacity 0.3s;
            pointer-events: none;
            z-index: 100;
        }

        .swipe-left {
            left: 0;
        }

        .swipe-right {
            right: 0;
        }
    </style>
</head>
<body style="background-color: black">
<a href="{{ url_for('index') }}" style="">← 返回相册列表</a>
<a href="#" onclick="event_full_screen()">全屏</a>
<div class="info-box">
    <span id="album-title">AI Waifus Art 🔞 Open Commissions!(AIWaifusArt)</span> •
    <span id="page-info">1/565</span>
    <input type="number"
           id="jump-input"
           min="1"
           max="565"
           value="1"
           placeholder="跳转"
           onchange="jumpToPage(this.value)">
</div>

<!-- 滑动指示器 -->
<div class="swipe-indicator swipe-left" id="swipe-left"></div>
<div class="swipe-indicator swipe-right" id="swipe-right"></div>

<div class="container">
    {% for image in images %}
        <div class="slide">
            <img src="{{ url_for('custom_static', filename=image.url) }}" alt="{{ image.filename }}" loading="lazy">
        </div>
    {% endfor %}
</div>

<script>
    let currentSlide = 0;
    const slides = document.querySelectorAll('.slide');
    const totalSlides = slides.length;

    function updatePageInfo() {
        document.getElementById('page-info').textContent = `${currentSlide}/${totalSlides}`;
    }

    // 新增跳转函数
    function jumpToPage(value) {
        let page = parseInt(value);
        if (isNaN(page)) return;
        page = Math.max(1, Math.min(page, totalSlides));
        showSlide(page - 1);
        document.getElementById('jump-input').value = page;  // 修正非法值
    }

    function showSlide(index) {
        // 限制切换范围
        if (index < 1) index = 1;
        if (index >= totalSlides) index = totalSlides - 1;
        if (index === currentSlide) return;

        slides.forEach(slide => slide.classList.remove('active'));
        currentSlide = index;
        slides[index - 1].classList.add('active');
        updatePageInfo();

        // 到达最后一张自动退出全屏
        if (currentSlide === totalSlides && document.fullscreenElement) {
            document.exitFullscreen();
        }
    }

    // 键盘控制
    document.addEventListener('keydown', (e) => {
        switch (e.key) {
            case 'ArrowLeft':
                showSlide(currentSlide - 1);
                break;
            case 'ArrowRight':
                showSlide(currentSlide + 1);
                break;
        }
    });

    // 滚轮控制
    document.addEventListener('wheel', (e) => {
        e.preventDefault();
        e.deltaY > 0 ? showSlide(currentSlide + 1) : showSlide(currentSlide - 1);
    }, {passive: false});

    // 全屏切换控制
    function event_full_screen() {
        // 忽略滑动操作产生的点击事件
        if (document.fullscreenElement) {
            document.exitFullscreen();
        } else {
            document.documentElement.requestFullscreen().catch(err => {
                console.log('全屏请求失败:', err);
            });
        }
    }

    {#
    document.addEventListener('click', (e) => {
        // 忽略滑动操作产生的点击事件
        if (e.isTrusted && !window.swiping) {
            if (document.fullscreenElement) {
                document.exitFullscreen();
            } else {
                document.documentElement.requestFullscreen().catch(err => {
                    console.log('全屏请求失败:', err);
                });
            }
        }
    });#}


    // 初始显示
    showSlide(1);
    // 触摸滑动控制
    let touchStartX = 0;
    let touchEndX = 0;

    document.addEventListener('touchstart', (e) => {
        console.log("touchstart")
        touchStartX = e.changedTouches[0].screenX;

        // 阻止默认行为，避免触发其他元素的点击
        if (e.target.tagName === 'IMG') {
            e.preventDefault();
        }
    }, {passive: false});

    document.addEventListener('touchmove', (e) => {
        console.log("touchmove")
        const currentX = e.changedTouches[0].screenX;
        const diff = currentX - touchStartX;

        // 显示滑动指示器
        if (diff > 30) {
            document.getElementById('swipe-right').style.opacity = '0.5';
            document.getElementById('swipe-left').style.opacity = '0';
        } else if (diff < -30) {
            document.getElementById('swipe-left').style.opacity = '0.5';
            document.getElementById('swipe-right').style.opacity = '0';
        }
    });

    document.addEventListener('touchend', (e) => {
        console.log("touchend")
        touchEndX = e.changedTouches[0].screenX;

        // 隐藏滑动指示器
        document.getElementById('swipe-left').style.opacity = '0';
        document.getElementById('swipe-right').style.opacity = '0';

        handleSwipe();
    });

    function handleSwipe() {
        const swipeThreshold = 50; // 滑动阈值，防止误触
        window.swiping = true; // 标记正在进行滑动操作

        if (touchEndX < touchStartX - swipeThreshold) {
            // 向左滑动，显示下一张
            showSlide(currentSlide + 1);
        }
        if (touchEndX > touchStartX + swipeThreshold) {
            // 向右滑动，显示上一张
            showSlide(currentSlide - 1);
        }

        // 滑动操作结束后重置标记
        setTimeout(() => {
            window.swiping = false;
        }, 300);
    }
</script>
</body>
</html>